package com.goods.business.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.HealthMapper;
import com.goods.business.service.HealthService;
import com.goods.common.model.business.Health;
import com.goods.common.vo.business.HealthVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class HealthServiceImpl implements HealthService {
    @Autowired
    private HealthMapper healthMapper;

    //今日是否已签到
    @Override
    public Boolean isReport() {
        //一天的开始或者结束时间
        Calendar c1 = new GregorianCalendar();
        c1.set(Calendar.HOUR_OF_DAY, 0);
        c1.set(Calendar.MINUTE, 0);
        c1.set(Calendar.SECOND, 0);
        String start = c1.getTime().toLocaleString();
        Calendar c2 = new GregorianCalendar();
        c2.set(Calendar.HOUR_OF_DAY, 23);
        c2.set(Calendar.MINUTE, 59);
        c2.set(Calendar.SECOND, 59);
        String end = c2.getTime().toLocaleString();

        Date date = new Date();
        Example example = new Example(Health.class);
        Example.Criteria criteria = example.createCriteria();
        HashMap<String, Object> map = new HashMap<>();
        map.put("startTime",start);
        map.put("endTime",end);

        if (map.get("startTime") != null && map.get("endTime") != null){
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            String s = (String) map.get("startTime");
            String e = (String) map.get("endTime");
            if (s != null && e != null){
                try {
                    Date startTime = df.parse(s);
                    Date endTime = df.parse(e);
                    criteria.andGreaterThan("createTime",startTime)
                            .andLessThan("createTime",endTime);
                } catch (ParseException ex) {
                    ex.printStackTrace();
                }
            }
        }

        Health health = healthMapper.selectOneByExample(example);
        if (health == null){
            return false;
        }else {
            return true;
        }
    }

    //立即打卡
    @Override
    public Boolean report(HealthVO healthVO) {
        Health health = new Health();
        BeanUtils.copyProperties(healthVO,health);
        health.setUserId(5L);
        health.setCreateTime(new Date());
        int insert = healthMapper.insert(health);
        if (insert == 1){
            return true;
        }else {
            return false;
        }
    }

    //显示打卡历史记录
    @Override
    public PageVO<HealthVO> history(Map<String, Object> map) {
        PageHelper.startPage(Integer.valueOf((String)map.get("pageNum")),
                Integer.valueOf((String) map.get("pageSize")));
        Example example = new Example(Health.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId",5);
        List<Health> list = healthMapper.selectByExample(example);
        List<HealthVO> listVO = list.stream().map(health -> {
            HealthVO healthVO = new HealthVO();
            BeanUtils.copyProperties(health, healthVO);
            return healthVO;
        }).collect(Collectors.toList());
        PageInfo<Health> pageInfo = new PageInfo<>(list);

        PageVO<HealthVO> pageVO = new PageVO<>(pageInfo.getTotal(), listVO);
        return pageVO;
    }
}
